﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using ImageTagToolViewModel.Models;
using ImageTagToolViewModel.Data;

namespace ImageTagToolViewModel.Serialization
{
    /// <summary>
    /// Class for serializing and deserializing pictures data
    /// </summary>
    public static class SerializationProvider
    {
        /// <summary>
        /// The default saving location.
        /// </summary>
        public const string Path = "pictures.xml";

        /// <summary>
        /// Serializes the list of pictures
        /// </summary>
        /// <param name="filename">name of the xml file</param>
        /// <param name="pictures">object which contains the list of pictures</param>
        public static void SerializePictures(string filename, PicturesXml pictures)
        {
            XmlSerializer x = new XmlSerializer(typeof(PicturesXml));
            // Ensuring that the StreamWriter resource will be released in any case.
            using (TextWriter writer = new StreamWriter(filename))
            {
                x.Serialize(writer, pictures);
            }
        }

        /// <summary>
        /// Serializes the data about the pictures.
        /// </summary>
        public static void SaveData()
        {
            if (PictureHandler.Instance.Pictures.Count > 0)
            {
                //Save to XML
                PicturesXml pictures = new PicturesXml();
                PictureHandler.Instance.Pictures.ForEach(x => pictures.pictureList.Add(x));
                pictures.LocationList.AddRange(LocationDatabase.Instance.ScannedLocations);
                SerializationProvider.SerializePictures(Path, pictures);

                //Save to DB
                DatabaseConnector dc = DatabaseConnector.Instance;
                dc.Connect();
                DatabaseOperationExecutor.SaveDataIntoDB(PictureHandler.Instance.Pictures, pictures.LocationList);
                dc.Disconnect();
            }
            
        }

        public static PicturesXml DeserializePictures(string filename)
        {
            PicturesXml pictures;
            XmlSerializer x = new XmlSerializer(typeof(PicturesXml));
            // Ensuring that the StreamWriter resource will be released in any case.
            using (StreamReader reader = new StreamReader(filename))
            {
                pictures = (PicturesXml)x.Deserialize(reader);
            }
            return pictures;
        }

        /// <summary>
        /// Deserializes the data about the pictures.
        /// </summary>
        public static void LoadData()
        {
            //Load from XML
            /*
            PicturesXml pictures = DeserializePictures(Path);
            PictureHandler.Instance.Pictures.AddRange(pictures.pictureList);
            PictureHandler.Instance.ActualPictureIndex = 0;
            LocationDatabase.Instance.InitializeWithRange(pictures.LocationList);
            TagDatabase.Instance.Restore(PictureHandler.Instance.Pictures);
             * */

            //Load from DB
            DatabaseConnector.Instance.Connect();
            PictureHandler.Instance.Pictures.AddRange(DatabaseOperationExecutor.LoadPicturesFromDB());
            PictureHandler.Instance.ActualPictureIndex = 0;
            LocationDatabase.Instance.InitializeWithRange(DatabaseOperationExecutor.LoadLocationsFromDB());
            TagDatabase.Instance.Restore(PictureHandler.Instance.Pictures);
            DatabaseConnector.Instance.Disconnect();
        }
    }
}
